package com.gomore.opple.utils;

import android.os.Environment;

import com.gomore.opple.OppleApplication;

import java.io.File;
import java.io.IOException;

public class FileUtils {
    public static final String IMAGE_CACHE = "cache";
    /**
     * SD卡是否存在
     **/
    private boolean hasSD = false;
    /**
     * SD卡的路径
     **/
    private String SDPATH;


    /**
     * 在SD卡上创建文件
     *
     * @throws IOException
     */
    public File createSDFile(String fileName) throws IOException {
        File file = new File(SDPATH + "/" + fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    /**
     * 判断SD是否可以
     *
     * @return
     */
    public static boolean isSdcardExist() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    public static String getRootFilePath() {
        if (isSdcardExist()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();   // filePath:/sdcard
        } else {
            return Environment.getDataDirectory().getAbsolutePath() + "/data/" + OppleApplication.getInstance().getPackageName() + File.separator; // filePath:
            // /data/data/
        }
    }

    public static File getQrCodePath(String userId) {
        String qrcodePath = getRootFilePath() + "qrcode/";
        createDirFile(qrcodePath);
        return createNewFile(qrcodePath + "/" + userId + ".png");
    }


    /**
     * 创建根目录
     *
     * @param path 目录路径
     */
    public static boolean createDirFile(String path) {
        File dir = new File(path);
        if (dir.exists()) {
            return true;
        }
        return dir.mkdirs();
    }

    public static void createDir(String... dirPath) {
        File dir = null;
        for (int i = 0; i < dirPath.length; i++) {
            dir = new File(dirPath[i]);
            if (!dir.exists() && !dir.isDirectory()) {
                dir.mkdirs();
            }
        }
    }

    public static String getImageFolder() {
        String rootFilePath = getRootFilePath();
        createDir(rootFilePath + IMAGE_CACHE);
        return rootFilePath + IMAGE_CACHE;
    }

    /**
     * 创建文件
     *
     * @param path 文件路径
     * @return 创建的文件
     */
    public static File createNewFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                return null;
            }
        }
        return file;
    }

    /**
     * 删除文件夹
     *
     * @param folderPath 文件夹的路径
     */
    public static void delFolder(String folderPath) {
        delAllFile(folderPath);
        String filePath = folderPath;
        filePath = filePath.toString();
        File myFilePath = new File(filePath);
        myFilePath.delete();
    }

    /**
     * 删除文件
     *
     * @param path 文件的路径
     */
    public static void delAllFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return;
        }
        if (!file.isDirectory()) {
            return;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);
                delFolder(path + "/" + tempList[i]);
            }
        }
    }

    //文件夹路径
    public static String getFirstFlodar(String string) {
        return getRootFilePath() + "/" + string + "/";
    }

    //文件夹路径
    public static String getSecondFlodar(String string, String uuid) {
        return getRootFilePath() + "/" + string + "/" + uuid + "/";
    }

    //文件路径
    public static String getUrlByFileName(String string, String fileName) {
        return getRootFilePath() + "/" + string + "/" + fileName;
    }

    //文件路径
    public static String getUrlByUuidAndFileName(String string, String uuid, String fileName) {
        return getRootFilePath() + "/" + string + "/" + uuid + "/" + fileName;
    }

    // 文件是否存在
    public static boolean isExit(String localFileUrl) {
        File dir = new File(localFileUrl);
        if (dir.exists()) {
            return true;
        } else {
            return false;
        }
    }
}
